package com.xiaoKe.zhonghzu.global.tool;

/**
 * @ProjectName : xiaoke-pad-zhongzhu
 * @Author : Shijingna
 * @Time : 2024/7/12 17:02
 * @Description : 文件描述
 */
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

public class SPUtils {
    private static final String SP_NAME = "config"; // SharedPreferences文件名
    private static SharedPreferences sp;

    /**
     * 初始化SharedPreferences
     *
     * @param context 上下文
     */
    static  Context mContext;
    public static void init(Context context) {
        mContext = context;
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        }
    }

    /**
     * 存放布尔值
     *
     * @param key   键
     * @param value 值
     */
    public static void putBoolean(String key, boolean value) {
        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * 读取布尔值
     *
     * @param key 键
     * @param def 默认值
     * @return 值
     */
    public static boolean getBoolean(String key, boolean def) {
        return sp.getBoolean(key, def);
    }

    /**
     * 存放字符串
     *
     * @param key   键
     * @param value 值
     */
    public static void putString(String key, String value) {
        Editor editor = sp.edit();
        editor.putString(key, value);
        editor.apply();
    }

    /**
     * 读取字符串
     *
     * @param key   键
     * @param def   默认值
     * @return 值
     */
    public static String getString(String key, String def) {
        return sp.getString(key, def);
    }

    /**
     * 存放整型
     *
     * @param key   键
     * @param value 值
     */
    public static void putInt(String key, int value) {
        Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    /**
     * 读取整型
     *
     * @param key   键
     * @param def   默认值
     * @return 值
     */
    public static int getInt(String key, int def) {
        return sp.getInt(key, def);
    }

    // 可以继续添加其他类型的方法，如putLong, getLong等

    public static <T> List<T> getDataList(String tag, Class<T> cls) {
        List<T> datalist = new ArrayList<T>();
        String strJson = sp.getString(tag, null);
        if (null == strJson) {
            return datalist;
        }
        LoggerTool.e("getDataList, json:" + strJson);
        try {
            Gson gson = new Gson();
            JsonArray array = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : array) {
                datalist.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            LoggerTool.e("Exception : " + e.getMessage());
        }
        return datalist;
    }
    public static <T> void putDataList(String tag, List<T> datalist) {
        if (null == datalist)
            return;

        Gson gson = new Gson();
        //change datalist to json
        String strJson = gson.toJson(datalist);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(tag, strJson);
        edit.commit();
    }

    public static <T> List<T> getList(String tag) {
        List<T> datalist = new ArrayList<T>();
        if (null == sp || null == tag) {
            return datalist;
        }
        String strJson = sp.getString(tag, "");
        if (null == strJson) {
            return datalist;
        }
        try {
            JsonArray array = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : array) {
                String asString = jsonElement.getAsString();
                datalist.add((T) asString);
            }
        } catch (Exception e) {
            LoggerTool.e("Exception : " + e.getMessage());
        }
        return datalist;
    }

    public static void clear( ) {
        if (sp == null) {
            sp = mContext.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sp.edit();
        edit.clear();
        edit.commit();
    }

}